package com.doctor.windflower_doctor.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.MessageAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.fragment.MenuFragment;
import com.doctor.windflower_doctor.fragment.MessageOne;
import com.doctor.windflower_doctor.fragment.MessageTwo;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class MessageActivity extends BaseActivity {
	private FragmentManager manager = null;
	private FragmentTransaction tran = null;
	private RelativeLayout bt_question, bt_system;
	private TextView tv_title, tv1, tv2;
	private ImageView iv_tishi;
	private ImageView iv_backs;
	private String requestUrl = Constent.URL
			+ "doctor/getMessages?doctorId="
			+ SharedPreferencesDB.getInstance(MessageActivity.this)
					.getDocotorId() + "&message=1&size=10&type=1";

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		bt_question.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				tran = manager.beginTransaction();
				MessageOne fragment1 = new MessageOne();
				bt_question.setSelected(true);
				tv1.setTextColor(Color.WHITE);
				bt_system.setSelected(false);
				tv2.setTextColor(Color.parseColor("#00aedf"));
				tran.replace(R.id.layout, fragment1, "fragment1");
				tran.commit();
			}
		});
		bt_system.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				bt_question.setSelected(false);
				iv_tishi.setVisibility(View.GONE);
				tv1.setTextColor(Color.parseColor("#00aedf"));
				bt_system.setSelected(true);
				tv2.setTextColor(Color.WHITE);
				tran = manager.beginTransaction();
				MessageTwo fragment2 = new MessageTwo();
				tran.replace(R.id.layout, fragment2, "fragment2");
				tran.commit();
			}
		});
		iv_backs.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (iv_tishi.getVisibility() == View.GONE) {
					Intent intent = new Intent(
							MainActivity.ISSHOWRED);
					
					sendBroadcast(intent);
				}

				finish();
			}
		});

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {// 返回键
			// 相关响应代码
			if (iv_tishi.getVisibility() == View.GONE) {
				Intent intent = new Intent(
						MainActivity.ISSHOWRED);
				
				sendBroadcast(intent);
			}

			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		manager = getSupportFragmentManager();
		tran = manager.beginTransaction();

		MessageOne fragment1 = new MessageOne();
		tran.add(R.id.layout, fragment1, "fragment1");
		tran.commit();
		bt_question = (RelativeLayout) findViewById(R.id.button1);
		bt_system = (RelativeLayout) findViewById(R.id.button2);
		tv_title = (TextView) findViewById(R.id.textView);
		tv_title.setText("消息通知");
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		iv_tishi = (ImageView) findViewById(R.id.iv_tishi);
		iv_backs = (ImageView) findViewById(R.id.back_btn);
		bt_question.setSelected(true);
		getUser(requestUrl);
	}

	private void getUser(String requestUrl) {
		HttpMethod requestMethod = HttpMethod.GET;
		HttpRequestCallBack<MessageAction> requestCallBack = new HttpRequestCallBack<MessageAction>(
				new JsonParser(), MessageAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<MessageAction> responseInfo) {
				// TODO Auto-generated method stub
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {
						if (responseInfo.success.data.hasNewSystemMsg) {
							iv_tishi.setVisibility(View.VISIBLE);
						}
					}
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				requestUrl, requestCallBack);
		HttpRequest.send(params);
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_message;
	}

}
